// userPackage/pages/orderDetail/orderDetail.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: '',
        path: app.globalData.path,

        parentId: 0,// 上级ID
        highestId: 0,// 上上级ID
        rebate: 0,//上级返利比例·
        rebates: 0,//上上级返利比例

        myVipFlag: 0,// 我的会员身份  0 不是会员  1 是会员 
        parentVipFlag: 0, // 上级会员身份
        highestVipFlag: 0, // 上上级会员身份

        isSpecialProduct: false

    },

    toPage(e) {
        console.log(e);
        var flag = e.currentTarget.dataset.flag,
            order = this.data.order;
        if (flag == 'pay') {
            // 立即支付
            this.toPay()
        } else if (flag == 'cancel') {
            // 取消订单
            console.log(order);
            this.cancelOrConfirmOrderHandle(order.mainId, 4)
        } else if (flag == 'wuLiu') {
            // 查看物流
            console.log(order);
            wx.navigateTo({
                url: '/userPackage/pages/orderWuLiu/orderWuLiu?orderNum=' + order.orderNumber,
            })

        } else if (flag == 'shouhuo') {
            // 确认收货
            this.cancelOrConfirmOrderHandle(order.mainId, 3)
        } else if (flag == 'pingjia') {
            // 去评价
            var obj = e.currentTarget.dataset.obj;
            console.log(obj);
            // return
            wx.navigateTo({
                url: '/packageProduct/pages/evaluate/evaluate?id=' + obj.productId
            })
        }
    },

    // 取消订单 确认收货
    cancelOrConfirmOrderHandle(orderId, status) {
        var url = app.globalData.url + "order/editOrderStatus";
        wx.showModal({
            title: '提示',
            content: status == 4 ? '是否操作该订单' : '是否确认收货',
            complete: (res) => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '',
                    })
                    app.wxRequest(url, {
                        mainId: orderId,
                        orderStatus: status
                    }, res => {
                        console.log(res);
                        if (res.flag) {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '操作成功',
                                showCancel: false,
                                complete: (res) => {
                                    if (res.confirm) {
                                        wx.redirectTo({
                                            url: '/packageProduct/pages/orderList/orderList?index=' + status,
                                        })
                                    }
                                }
                            })

                        } else {
                            wx.showToast({
                                title: '操作失败，请重试',
                                icon: 'none'
                            })
                        }
                    },
                        err => {
                            console.log(err);
                        })
                }
            }
        })
    },
    copyHandle(e) {
        var text = e.currentTarget.dataset.text;
        wx.setClipboardData({
            data: text,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: "none",
                    mask: "true"
                })
            }
        })
    },






    toPay() {
        wx.showLoading({
            title: '',
        })
        var isSpecialProduct = this.data.isSpecialProduct, url = app.globalData.url + (isSpecialProduct ? 'pay/payMiniOrder' : 'pay/payMiniProductOrder'), order = this.data.order, rebate = this.data.rebate, rebateMoney = 0, rebates = this.data.rebates, rebatesMoney = 0, that = this;
        console.log(rebate);
        console.log(rebates);
        rebateMoney = rebate * order.totalMoney
        rebatesMoney = rebates * order.totalMoney

        console.log(rebateMoney);
        console.log(rebatesMoney);
        var data = {
            userId: app.globalData.userId,
            shopId: app.globalData.shopId,
            orderNumber: order.orderNumber,
            realMoney: order.realMoney,
            flag: 1,
            openId: app.globalData.openId,
            ifDown: 0,
        }
        if (isSpecialProduct) {
            data.highestId = that.data.highestId || 0
            data.parentId = that.data.parentId || 0
            data.rebate = that.data.parentVipFlag == 1 ? rebateMoney || 0 : 0
            data.rebates = that.data.highestVipFlag == 1 ? rebatesMoney || 0 : 0
            data.parentNote = that.data.parentVipFlag || 0
            data.highestNote = that.data.highestVipFlag || 0
            data.note = that.data.myVipFlag || 0
        } else {
            data.usedYh = order.usedYh
        }

        console.log(order);
        app.wxRequest(url, data, res => {
            console.log(res);
            wx.hideLoading()
            if (res.flag) {
                var datas = res.data.finalPackage;
                console.log(datas);
                wx.requestPayment({
                    "timeStamp": datas.timeStamp,
                    "nonceStr": datas.nonceStr,
                    "package": datas.package,
                    "signType": datas.signType,
                    "paySign": datas.paySign,
                    "success": function (res) {
                        console.log(res);
                        if (res.errMsg == "requestPayment:ok") {
                            //支付成功
                            wx.showModal({
                                title: '提示',
                                content: '立即查看',
                                showCancel: false,
                                complete: (res) => {
                                    if (res.confirm) {
                                        //  前往订单页
                                        wx.redirectTo({
                                            url: '/packageProduct/pages/orderList/orderList?index=' + 1,
                                        })
                                    }
                                }
                            })
                        }
                    },
                    "fail": function (res) {
                        console.log(res);
                        wx.showToast({
                            title: '支付失败,请重试',
                            icon: 'none'
                        })
                    },
                    "complete": function (res) {
                        console.log(res);
                    }
                })
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        },
            err => {
                console.log(err);
            })
    },
    // 获取我的上级 上上级id
    getUserParentId() {
        wx.showLoading({
            title: '加载中',
        })
        var that = this, url = app.globalData.url + "points/listHigherUser",
            data = {
                userId: app.globalData.userId,
            };
        app.wxRequest(url, data, res => {
            console.log(res.data);
            if (res.flag) {
                wx.hideLoading()
                that.setData({
                    parentId: res.data.pater != null ? res.data.pater.userId : 0,
                    parentVipFlag: res.data.pater != null ? res.data.pater.note : 0,
                    highestId: res.data.grandsire != null ? res.data.grandsire.userId : 0,
                    highestVipFlag: res.data.grandsire != null ? res.data.grandsire.note : 0
                })
                console.log(that.data);
            }
        })
    },
    getUserData() {
        wx.showLoading({
            title: '加载中',
        })
        var that = this, myVipFlag = 0, url = app.globalData.url + "points/listLowerUser",
            data = {
                userId: app.globalData.userId,
            };
        app.wxRequest(url, data, res => {
            console.log(res.data);
            if (res.flag) {
                wx.hideLoading()
                if (res.data != null) {
                    myVipFlag = res.data.user.note
                }
                that.setData({
                    myVipFlag
                })
            }
        })
    },

    // 获取返利比例
    getRebate() {
        wx.showLoading({
            title: '加载中',
        })
        var that = this, url = app.globalData.url + "points/listSetting",
            data = {};
        app.wxRequest(url, data, res => {
            console.log(res.data);
            if (res.flag) {
                wx.hideLoading()
                that.setData({
                    rebate: res.data.levelOnePercentage,
                    rebates: res.data.levelTwoPercentage,
                })
                that.getUserData()
                console.log(that.data);
            }
        })


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);

        var isSpecialProduct = false, order = JSON.parse(options.order);
        this.setData({
            order,
            isSpecialProduct
        })

        if (isSpecialProduct) {
            this.getUserParentId()
            this.getRebate()
        }

        console.log(this.data);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})